package com.eshop.service;

import com.eshop.mapper.RoleMapper;
import com.eshop.mapper.UserMapper;
import com.eshop.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    //根据用户id查询权限列表
    public List<Role> getRoles(Integer uid){ return roleMapper.getRoles(uid);}

    //按用户名查询user
    public User queryUserByName(String name){ return userMapper.queryUserByName(name);}

    //禁用用户
    public boolean banUserById(Integer id) { return userMapper.banUserById(id); }

    //解禁用户
    public boolean reuseUserById(Integer id) { return userMapper.reuseUserById(id); }

    //更新用户信息
    public boolean updateById(User user) { return userMapper.updateByPrimaryKeySelective(user);}

    //通过id删除用户信息
    public boolean removeById(Integer id) { return userMapper.deleteByPrimaryKey(id); }

    //更新用户密码
    public RespBean updateUserPassword(String oldPass,String pass,Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //判断旧密码是否正确，若正确则获取新密码并更新
        if (encoder.matches(oldPass, user.getPassword())) {
            String encodePass = encoder.encode(pass);
            Integer result = userMapper.updatePasswd(userId, encodePass);
            if (result == 1) {
                return RespBean.success("更新成功！");
            }
        }
        return RespBean.error("更新失败！");
    }
    public RespPageBean getUserListByPage(Integer currentPage, Integer size, User user) {
        PageHelper.startPage(currentPage,size);
        List<User> userList =  userMapper.getUserListByPage(user);
        for (User user1 : userList) {
            user1.setRoles(getRoles(user1.getId()));
            user1.setPassword(null);
        }
        PageInfo<User> data = new PageInfo<User>(userList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }

    //更新用户头像
    public RespBean updateUserFace(String url, Integer id, Authentication authentication) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setUserFace(url);
        boolean result = userMapper.updateByPrimaryKeySelective(user);
        if (result){
            User principal = (User) authentication.getPrincipal();
            //更新全局信息
            principal.setUserFace(url);
            SecurityContextHolder.getContext().setAuthentication
                    (new UsernamePasswordAuthenticationToken(user,null,authentication.getAuthorities()));
            return RespBean.success("上传成功!",url);
        }
        return RespBean.error("更新失败!",url);

    }


    //    //查询所有用户列表
//    public List<User> queryUserList(){ return userMapper.queryUserList();}
}
